package dao;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

import model.Operacao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.util.Log;

/**
 * @author Gonze
 * 
 */

public class OperacaoDAO extends MovimentacaoDAO {

	private String tableName = "Operacao";

	public OperacaoDAO(Context ctx) {
		super(ctx);
	}

	public long insert(Operacao object) {
		super.database.beginTransaction();

		long idMovimentacao = super.insert(object);

		ContentValues content = new ContentValues();
		content.put("idmovimentacao", idMovimentacao);
		content.put("idcategoria", object.getCategoria().getIdCategoria());
		content.put("descricao", object.getDescricao());
		content.put("Anexo", object.getAnexo());

		long id = database.insert(tableName, null, content);
		super.database.setTransactionSuccessful();
		super.database.endTransaction();

		return id;
	}

	public void update(Operacao object) {

		super.database.beginTransaction();

		try {
			ContentValues content = new ContentValues();
			// linhas afetadas
			int count = super.update(object);
			if (count > 0) {
				content.put("idcategoria", object.getCategoria()
						.getIdCategoria());
				content.put("descricao", object.getDescricao());
				content.put("Anexo", object.getAnexo());
				super.database.update(tableName, content,
						"where idmovimentacao = ?",
						new String[] { String.valueOf(object.getId()) });
				super.database.setTransactionSuccessful();
			}
		} catch (SQLException e) {
			Log.e(this.getClass().getName() + "_update", e.toString());
		} finally {
			super.database.endTransaction();
		}
	}

	public void delete(Operacao object) {

		try {
			int count = database.delete(tableName, "where idmovimentacao = ?",
					new String[] { String.valueOf(object.getId()) });

			if (count > 0) {
				super.delete(object);
				super.database.setTransactionSuccessful();
			}
		} catch (SQLException e) {
			Log.e(this.getClass().getName() + "_delete", e.toString());
		} finally {
			super.database.endTransaction();
		}
	}

	public List<Operacao> getList() {
		List<Operacao> lista = new ArrayList<Operacao>();

		Cursor cursor = null;

		String sql = "select "
				+ this.tableName
				+ ".idmovimentacao, "
				+ " titulo, valor, dataoperacao, tipooperacao, idcategoria, descricao, anexo  from "
				+ this.tableName + " join " + super.tableName + " on "
				+ super.tableName + ".idmovimentacao = " + this.tableName
				+ ".idmovimentacao ";

		cursor = super.database.rawQuery(sql, null);

		for (int i = 0; i < cursor.getCount(); i++) {
			cursor.moveToPosition(i);
			Operacao operacao = new Operacao();
			operacao.setId(cursor.getInt(0));
			operacao.setTitulo(cursor.getString(1));
			operacao.setValor(cursor.getDouble(2));
			operacao.setDataOperacao(Date.valueOf(cursor.getString(3)));
			operacao.setTipoOperacao(cursor.getInt(4));
			CategoriaDAO categoriaDAO = new CategoriaDAO(super.context);
			operacao.setCategoria(categoriaDAO.getCategoria(cursor.getInt(5)));
			operacao.setDescricao(cursor.getString(6));
			operacao.setAnexo(cursor.getBlob(7));
			lista.add(operacao);
		}
		return lista;
	}

}
